﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyHealthOnline.Business;

namespace MyHealthOnline.Administrators
{
    public partial class ManagePatients : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void grdPatients_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int index = Convert.ToInt32(e.CommandArgument);
            if (e.CommandName == "Add")
            {
                TextBox txtNewName = grdPatients.FooterRow.FindControl("txtNewName") as TextBox;
                TextBox txtNewPassword = grdPatients.FooterRow.FindControl("txtNewPassword") as TextBox;
                TextBox txtNewNumber = grdPatients.FooterRow.FindControl("txtNewNumber") as TextBox;
                TextBox txtNewEmail = grdPatients.FooterRow.FindControl("txtNewEmail") as TextBox;
                TextBox txtNewAddress = grdPatients.FooterRow.FindControl("txtNewAddress") as TextBox;
                TextBox txtNewCity = grdPatients.FooterRow.FindControl("txtNewCity") as TextBox;
                TextBox txtNewCountry = grdPatients.FooterRow.FindControl("txtNewCountry") as TextBox;
                TextBox txtNewPhone = grdPatients.FooterRow.FindControl("txtNewPhone") as TextBox;
                TextBox txtNewBirthday = grdPatients.FooterRow.FindControl("txtNewBirthday") as TextBox;
                TextBox txtNewSex = grdPatients.FooterRow.FindControl("txtNewSex") as TextBox;

                PatientBusiness.CreatePatient(txtNewName.Text, txtNewPassword.Text, txtNewNumber.Text, txtNewEmail.Text,
                           txtNewAddress.Text, txtNewCity.Text, txtNewCountry.Text, txtNewPhone.Text, txtNewBirthday.Text,
                           txtNewSex.Text);
                grdPatients.DataBind();

                txtNewName.Text = "";
                txtNewPassword.Text = "";
                txtNewNumber.Text = "";
                txtNewEmail.Text = "";
                txtNewAddress.Text = "";
                txtNewCity.Text = "";
                txtNewCountry.Text = "";
                txtNewPhone.Text = "";
                txtNewBirthday.Text = "";
                txtNewSex.Text="";

                updPatients.Update();

                WebServiceHO webService = new WebServiceHO();
                webService.Notify("administrator@webconsult.gmail", txtNewEmail.Text,
                    "Your account on web consult is created. You have the password " + txtNewPassword.Text,
                    "Account created");
            }   
            else
            if (e.CommandName == "Select")
            {
                //int id = Convert.ToInt32(grdPatients.DataKeys[index].Value);
                grdPatients.EditIndex = index;
            }
            else if (e.CommandName == "Edit")
            {
                grdPatients.EditIndex = index;
            }
            else if (e.CommandName == "Update")
            {
                TextBox txtName = (TextBox)grdPatients.Rows[index].FindControl("txtName");
                TextBox txtPassword = (TextBox)grdPatients.Rows[index].FindControl("txtPassword");
                TextBox txtNumber = (TextBox)grdPatients.Rows[index].FindControl("txtNumber");
                TextBox txtEmail = (TextBox)grdPatients.Rows[index].FindControl("txtEmail");
                TextBox txtAddress = (TextBox)grdPatients.Rows[index].FindControl("txtAddress");
                TextBox txtCity = (TextBox)grdPatients.Rows[index].FindControl("txtCity");
                TextBox txtCountry = (TextBox)grdPatients.Rows[index].FindControl("txtCountry");
                TextBox txtPhone = (TextBox)grdPatients.Rows[index].FindControl("txtPhone");
                TextBox txtBirthday = (TextBox)grdPatients.Rows[index].FindControl("txtBirthday");
                TextBox txtSex = (TextBox)grdPatients.Rows[index].FindControl("txtSex");
                int id = Convert.ToInt32(grdPatients.DataKeys[index].Value);

                PatientBusiness.UpdatePatient(id, txtName.Text, txtPassword.Text, txtNumber.Text, txtEmail.Text,
                          txtAddress.Text, txtCity.Text, txtCountry.Text, txtPhone.Text, txtBirthday.Text,
                          txtSex.Text);

                grdPatients.EditIndex = -1;
                grdPatients.DataBind();

                WebServiceHO webService = new WebServiceHO();
                webService.Notify("administrator@webconsult.gmail", txtEmail.Text,
                    "Your account on web consult is updated. You have the password " + txtPassword.Text,
                    "Account updated");

                updPatients.Update();
            }
            else if (e.CommandName == "Cancel")
            {
                grdPatients.EditIndex = -1;
            }
            else if (e.CommandName == "Delete")
            {
                int id = Convert.ToInt32(grdPatients.DataKeys[index].Value);
                PatientBusiness.DeletePatient(id);
                grdPatients.DataBind();
                updPatients.Update();
            }
        }

        protected void grdPatients_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            grdPatients.EditIndex = -1;
            grdPatients.DataBind();
            updPatients.Update();
        }
      
    }
}